package cn.hahah.module.blog.service.userfocus;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.userfocus.vo.*;
import cn.hahah.module.blog.dal.dataobject.userfocus.UserFocusDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.userfocus.UserFocusMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户关注 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserFocusServiceImpl implements UserFocusService {

    @Resource
    private UserFocusMapper userFocusMapper;

    @Override
    public Long createUserFocus(UserFocusSaveReqVO createReqVO) {
        // 插入
        UserFocusDO userFocus = BeanUtils.toBean(createReqVO, UserFocusDO.class);
        userFocusMapper.insert(userFocus);
        // 返回
        return userFocus.getId();
    }

    @Override
    public void updateUserFocus(UserFocusSaveReqVO updateReqVO) {
        // 校验存在
        validateUserFocusExists(updateReqVO.getId());
        // 更新
        UserFocusDO updateObj = BeanUtils.toBean(updateReqVO, UserFocusDO.class);
        userFocusMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserFocus(Long id) {
        // 校验存在
        validateUserFocusExists(id);
        // 删除
        userFocusMapper.deleteById(id);
    }

    private void validateUserFocusExists(Long id) {
        if (userFocusMapper.selectById(id) == null) {
            throw exception(USER_FOCUS_NOT_EXISTS);
        }
    }

    @Override
    public UserFocusDO getUserFocus(Long id) {
        return userFocusMapper.selectById(id);
    }

    @Override
    public PageResult<UserFocusDO> getUserFocusPage(UserFocusPageReqVO pageReqVO) {
        return userFocusMapper.selectPage(pageReqVO);
    }

}